package com.demo.auth.service.security;

import com.demo.auth.service.remote.DispatchRemoteAuthUserService;
import com.demo.user.vo.AuthUserVO;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import static java.util.Collections.emptyList;

/**
 * @author zhaoxinguo on 2017/9/13.
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    private final DispatchRemoteAuthUserService dispatchRemoteAuthUserService;

    /**
     * 通过构造器注入UserRepository
     * @param dispatchRemoteAuthUserService 构造注入
     */
    public UserDetailsServiceImpl(DispatchRemoteAuthUserService dispatchRemoteAuthUserService) {
        this.dispatchRemoteAuthUserService = dispatchRemoteAuthUserService;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        AuthUserVO authUserVO = dispatchRemoteAuthUserService.getUserByUsername(username);
        if (authUserVO == null) {
            throw new UsernameNotFoundException("当前用户不存在!");
        }
        return new org.springframework.security.core.userdetails.User(authUserVO.getUsername(), authUserVO.getPassword(), emptyList());
    }

}
